package com.sa07.ui.model;

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

import com.sa07.resources.ResourceUtils;
import com.sa07.ui.enums.TestResult;

/**
 * 测试表格单元格渲染类
 * 
 * @author Long.shangbo
 *
 * 2014-2-18 上午12:14:41
 */
public class TestTableCellRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = -276398811692955449L;
	
	private static final String TEST_SUCCESS_ICON = "test-success.gif";

	private static final String TEST_FAILED_ICON = "test-failed.gif";
	

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {
		Component c = super.getTableCellRendererComponent(table, value,
				isSelected, hasFocus, row, column);
		//默认是Label
		JLabel currComponent = (JLabel) c;
		
		//以相应的图片来显示测试结果
		if(value instanceof TestResult){
			setHorizontalAlignment(CENTER);
			currComponent.setText("");
			TestResult testResult = (TestResult) value;
			switch (testResult) {
				case SUCCESS:
					currComponent.setIcon(ResourceUtils.getIcon(TEST_SUCCESS_ICON));
					break;
				case FAILED:
					currComponent.setIcon(ResourceUtils.getIcon(TEST_FAILED_ICON));
					break;
			}
		}
		
		//编号列设置居中
		TableColumn tableColumn = table.getTableHeader().getColumnModel().getColumn(column);
		
		if(tableColumn.getHeaderValue().toString().equals(DefaultTestTableModel.TEST_ID)){
			setHorizontalAlignment(CENTER);
		}
		return c;
	}
}
